﻿using mozhi.smarterp.Finance.Banks.Repository;
using mozhi.smarterp.Finance.Basedatas;
using mozhi.smarterp.Finance.Basedatas.Repository;
using mozhi.smarterp.Finance.Querys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Guids;

namespace mozhi.smarterp.Finance.Banks
{
    public class BankTypeAppService : FinanceAppService, IBankTypeAppService
    {
        private IBankTypeRepository _bankTypeRepository;
        private IGuidGenerator _guidGenerator;

        public BankTypeAppService(IBankTypeRepository bankTypeRepository, IGuidGenerator guidGenerator, IDataAssociationRepository dataAssociationRepository)
            : base(dataAssociationRepository)
        {
            _bankTypeRepository = bankTypeRepository;
            _guidGenerator = guidGenerator;
        }

        public async Task<PagedResultDto<BankTypeDto>> FindAsync(PageQuery query)
        {
            var bankTypes = await _bankTypeRepository.FindAsync(query);

            var count = await _bankTypeRepository.GetCountAsync(query);

            var result = new PagedResultDto<BankTypeDto>();

            var data = ObjectMapper.Map<List<BankType>, List<BankTypeDto>>(bankTypes);

            result.TotalCount = count;
            result.Items = data as IReadOnlyList<BankTypeDto>;

            return result;
        }

        public async Task<BankTypeDto> GetAsync(Guid id)
        {
            var bankType = await _bankTypeRepository.FindAsync(id);

            return ObjectMapper.Map<BankType, BankTypeDto>(bankType);
        }


        public async Task<BankTypeDto> CreateAsync(BankTypeCreateDto dto)
        {
            var nameExistCount = await _bankTypeRepository.GetCountAsync(new PageQuery() { Keyword = dto.Name, PageIndex = 1, PageSize = int.MaxValue });

            if (nameExistCount > 0)
                throw new UserFriendlyException("银行名称已存在", "100000");

            var bankType = new BankType(_guidGenerator.Create(), dto.Name);

            bankType = await _bankTypeRepository.InsertAsync(bankType);

            return ObjectMapper.Map<BankType, BankTypeDto>(bankType);
        }

        public async Task<BankTypeDto> UpdateAsync(BankTypeUpdateDto dto)
        {
            var bankType = await _bankTypeRepository.GetAsync(dto.Id);

            if (bankType == null)
                throw new UserFriendlyException("银行类型不存在", "100000");

            var nameExistCount = await _bankTypeRepository.GetCountAsync(new PageQuery() { Keyword = dto.Name, PageIndex = 1, PageSize = int.MaxValue, ExclusionId = dto.Id });

            if (nameExistCount > 0)
                throw new UserFriendlyException("银行类型名称已存在", "100000");

            bankType.Change(dto.Name, dto.Activated);

            bankType = await _bankTypeRepository.UpdateAsync(bankType);

            return ObjectMapper.Map<BankType, BankTypeDto>(bankType);

        }

        public async Task<bool> DeleteAsync(Guid id)
        {
            var bankType = await _bankTypeRepository.GetAsync(id);

            if (bankType == null)
                throw new UserFriendlyException("银行类型不存在", "100000");

            await CheckDataAssociation(id);

            await _bankTypeRepository.DeleteAsync(bankType);

            return true;
        }
    }
}
